using System;

namespace Meta
{
	/// <summary>
	/// An IDictionary specialized to Types that obeys inheritance.
	/// </summary>
	public class TypeTable : System.Collections.Hashtable {
		public TypeTable() : base()
		{
		}

		Type FindKey(Type t) {
			// Search type hierarchy
			Type search = t;
			do {
				if (ContainsKey(search))
					return search;
				search = search.BaseType;
			} while (search!=typeof(System.Object));
			// Search interfaces
			foreach (Type i in t.GetInterfaces())
				if (ContainsKey(i))
					return i;
			return null;
		}

		public override object this[object t] {
			get {
				Type probe = FindKey((Type)t);
				if (probe!=null)
					return base[probe];
				else
					return null;
			}
			set {
				base[t] = value;
			}
		}

	}
}
